using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Spinning.Spinning.Unitity;
using Spinning.Spinning.Entity;
using Microsoft.Xna.Framework;

namespace Spinning.Spinning.Scenes.ProcessScene.TopMembers
{
    class MemberContainer : BaseScene
    {
        public delegate void OnChangeMember(Constants.PlayerId player);
        public OnChangeMember OnChangeMemberEvent;

        List<Member> members = new List<Member>();
        DirectionKey right, left;
        Member _currentMember;
        public Member CurrentMember
        {
            get
            {
                return _currentMember;
            }
            protected set
            {
                _currentMember = value;
                //Game1.stageManager.GameSrate.CurrentPlayer = _currentMember.Player;
            }
        }

        public void SetMemberByPlayer(Constants.PlayerId player)
        {
            CurrentMember = findCurrentMember(player);
        }

        public MemberContainer()
        {
            Size = new Microsoft.Xna.Framework.Vector2(426, 168);
            ZIndex = Constants.ui;
            Color = Color.Transparent;
        }

        private Member findCurrentMember(Constants.PlayerId Id)
        {
            for (int i = 0; i < members.Count; i++)
            {
                if (members[i].Player == Id) return members[i];
            }
            return null;
        }

        public override void OnLoad()
        {
            base.OnLoad();
            right = new DirectionKey(this);
            right.direction = DirectionKey.Direction.Right;
            left = new DirectionKey(this);
            left.direction = DirectionKey.Direction.Left;
            right.Origin = new Vector2(.5f);
            left.Origin = new Vector2(.5f);

            Vector2 pos = new Vector2(213, 84);
            float x = 180;
            left.Position = pos - new Vector2(x, 0);
            right.Position = pos + new Vector2(x, 0);

            right.OnLoad();
            left.OnLoad();

            Attach(right);
            Attach(left);

            TouchManager.RegisteTouchArea(right);
            TouchManager.RegisteTouchArea(left);

            //left.IsEnd = true;
        }

        public void AddMember(Member m)
        {
            members.Add(m);
            Attach(m);
            CurrentMember = m;
            m.Position = Size / 2;
        }

        protected override void UpdateSelf(GameTime gameTime)
        {
            base.UpdateSelf(gameTime);

            foreach (Member m in members)
            {
                m.Visable = (CurrentMember == m);
            }

            if (CurrentMember != null)
            {
                int i = members.IndexOf(CurrentMember);
                if (i == 0)
                {
                    left.IsEnd = true;
                    right.IsEnd = false;
                }
                else if (i == members.Count - 1)
                {
                    left.IsEnd = false;
                    right.IsEnd = true;
                }
                else
                {
                    left.IsEnd = false;
                    right.IsEnd = false;
                }
                if (members.Count == 1)
                    left.IsEnd = right.IsEnd = true;
            }
        }

        public void OnKeyTouch(DirectionKey.Direction d)
        {
            int i = members.IndexOf(CurrentMember);
            if (d == DirectionKey.Direction.Left && i > 0)
            {
                i--;
            }
            else if (d == DirectionKey.Direction.Right && i < (members.Count - 1))
            {
                i++;
            }
            CurrentMember = members[i];
            if (OnChangeMemberEvent != null)
            {
                OnChangeMemberEvent(CurrentMember.Player);
            }
        }

        public override BaseScene Clone()
        {
            throw new NotImplementedException();
        }
    }
}
